package cn.com.contec.bean;

import java.io.File;

import cn.com.contec.db.DBOpenHelper;
import cn.com.contec.util.HardWareUtil;
import cn.com.contec.util.ICommTConstant;


import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.net.Uri;
import android.util.Log;


public class EcgFileInfoMethods {
	private DBOpenHelper dOpenHelper;

	public EcgFileInfoMethods(Activity activity) {
		this.dOpenHelper = new DBOpenHelper(activity);
	}

	/**
	 * 添加病历对应的心电文�?
	 * 
	 * 对应病历的_id 要查出来
	 * 
	 * 
	 * 
	 * @param returnUri
	 *            �?��病历的uri 获取_id
	 * @param saveFileName
	 * @return
	 */
	public static ContentValues getNewEcgFileInfo(Uri returnUri,
			String saveFileName) {
		String P_ID = returnUri.getPathSegments().get(1);
		if (Integer.valueOf(P_ID) < 0) {
			return null;
		}
		String filePath = HardWareUtil.getEcgFilePath();
		String fileAdd = filePath + "/" + saveFileName ;
		ContentValues values = new ContentValues();
		values.put(EcgFile.FILE_ADD, fileAdd);
		//values.put(EcgFile.FILE_LENGTH, value)
		return values;
	}

	public static String getDeleteByP_ID(Uri noteUri) {
		// TODO Auto-generated method stub
		try {
			String id = noteUri.getPathSegments().get(1);
			return id;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

	}

	/*
	 * 
	 */
	public File getEcgFile(String p_ID) {
		// TODO Auto-generated method stub
		String filePath = getFileNameByP_ID(p_ID);
		if (filePath != null && !"".equals(filePath)) {
			return new File(filePath);
		} else {
			return null;
		}
	}

	/**
	 * 
	 * 
	 * @param deleteEcgFileP_ID
	 * @return
	 */
	public String getFileNameByP_ID(String deleteEcgFileP_ID) {
		SQLiteDatabase db = dOpenHelper.getReadableDatabase();
		String sql = "SELECT * FROM " + ICommTConstant.CONTACTS_ECG_FILE_TABLE
				+ " WHERE " + EcgFile._ID + " = " + deleteEcgFileP_ID.trim();
		Log.i("===========================", sql);
		Cursor cursor = db.rawQuery(sql, null);
		cursor.moveToFirst();
		String filePath = cursor.getString(EcgFile.FILE_ADD_COLUMN);
		cursor.close();
		return filePath;
		
	}

	/**
	 * 通过pid找到文件路径 然后在sd卡中删除文件
	 * 
	 * @param deleteEcgFileP_ID
	 * @return true yes false no
	 * 
	 */
	public boolean deleteEcgFileByP_ID(String fileName) {
		// TODO Auto-generated method stub
		boolean delete = false;
		if (fileName != null) {
			File file = new File(fileName);
			delete = deleteFile(file);
			
		}
		
		return delete;
	}

	public boolean deleteFile(File file) {
		// TODO Auto-generated method stub
		boolean yes = false;
		if (file.exists()) {
			yes = file.delete();
			
		}
		return yes;
	}
}
